home *** CD-ROM | disk | FTP | other *** search
Wrap
MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) NNNNAAAAMMMMEEEE Math::Trig - trigonometric functions SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS use Math::Trig; $x = tan(0.9); $y = acos(3.7); $z = asin(2.4); $halfpi = pi/2; $rad = deg2rad(120); DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN Math::Trig defines many trigonometric functions not defined by the core Perl which defines only the sin() and cos(). The constant ppppiiii is also defined as are a few convenience functions for angle conversions. TTTTRRRRIIIIGGGGOOOONNNNOOOOMMMMEEEETTTTRRRRIIIICCCC FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS The tangent ttttaaaannnn The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot are aliases) ccccsssscccc, ccccoooosssseeeecccc, sssseeeecccc, sssseeeecccc, ccccooootttt, ccccoooottttaaaannnn The arcus (also known as the inverse) functions of the sine, cosine, and tangent aaaassssiiiinnnn, aaaaccccoooossss, aaaattttaaaannnn The principal value of the arc tangent of y/x aaaattttaaaannnn2222(y, x) The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and acotan/acot are aliases) aaaaccccsssscccc, aaaaccccoooosssseeeecccc, aaaasssseeeecccc, aaaaccccooootttt, aaaaccccoooottttaaaannnn The hyperbolic sine, cosine, and tangent ssssiiiinnnnhhhh, ccccoooosssshhhh, ttttaaaannnnhhhh The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch and cotanh/coth are aliases) PPPPaaaaggggeeee 1111 MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) ccccsssscccchhhh, ccccoooosssseeeecccchhhh, sssseeeecccchhhh, ccccooootttthhhh, ccccoooottttaaaannnnhhhh The arcus (also known as the inverse) functions of the hyperbolic sine, cosine, and tangent aaaassssiiiinnnnhhhh, aaaaccccoooosssshhhh, aaaattttaaaannnnhhhh The arcus cofunctions of the hyperbolic sine, cosine, and tangent (acsch/acosech and acoth/acotanh are aliases) aaaaccccsssscccchhhh, aaaaccccoooosssseeeecccchhhh, aaaasssseeeecccchhhh, aaaaccccooootttthhhh, aaaaccccoooottttaaaannnnhhhh The trigonometric constant ppppiiii is also defined. $pi2 = 2 * ppppiiii; EEEERRRRRRRROOOORRRRSSSS DDDDUUUUEEEE TTTTOOOO DDDDIIIIVVVVIIIISSSSIIIIOOOONNNN BBBBYYYY ZZZZEEEERRRROOOO The following functions acoth acsc acsch asec asech atanh cot coth csc csch sec sech tan tanh cannot be computed for all arguments because that would mean dividing by zero or taking logarithm of zero. These situations cause fatal runtime errors looking like this cot(0): Division by zero. (Because in the definition of cot(0), the divisor sin(0) is 0) Died at ... or atanh(-1): Logarithm of zero. Died at... For the csc, cot, asec, acsc, acot, csch, coth, asech, acsch, the argument cannot be 0 (zero). For the atanh, acoth, the argument cannot be 1 (one). For the atanh, acoth, the argument cannot be -1 (minus one). For the tan, sec, tanh, sech, the argument cannot be _p_i/_2 + _k * _p_i, where PPPPaaaaggggeeee 2222 MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) _k is any integer. SSSSIIIIMMMMPPPPLLLLEEEE ((((RRRREEEEAAAALLLL)))) AAAARRRRGGGGUUUUMMMMEEEENNNNTTTTSSSS,,,, CCCCOOOOMMMMPPPPLLLLEEEEXXXX RRRREEEESSSSUUUULLLLTTTTSSSS Please note that some of the trigonometric functions can break out from the rrrreeeeaaaallll aaaaxxxxiiiissss into the ccccoooommmmpppplllleeeexxxx ppppllllaaaannnneeee. For example asin(2) has no definition for plain real numbers but it has definition for complex numbers. In Perl terms this means that supplying the usual Perl numbers (also known as scalars, please see the _p_e_r_l_d_a_t_a manpage) as input for the trigonometric functions might produce as output results that no more are simple real numbers: instead they are complex numbers. The Math::Trig handles this by using the Math::Complex package which knows how to handle complex numbers, please see the _M_a_t_h::_C_o_m_p_l_e_x manpage for more information. In practice you need not to worry about getting complex numbers as results because the Math::Complex takes care of details like for example how to display complex numbers. For example: print asin(2), "\n"; should produce something like this (take or leave few last decimals): 1.5707963267949-1.31695789692482i That is, a complex number with the real part of approximately 1.571 and the imaginary part of approximately -1.317. PPPPLLLLAAAANNNNEEEE AAAANNNNGGGGLLLLEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS (Plane, 2-dimensional) angles may be converted with the following functions. $radians = deg2rad($degrees); $radians = grad2rad($gradians); $degrees = rad2deg($radians); $degrees = grad2deg($gradians); $gradians = deg2grad($degrees); $gradians = rad2grad($radians); The full circle is 2 _p_i radians or _3_6_0 degrees or _4_0_0 gradians. RRRRAAAADDDDIIIIAAAALLLL CCCCOOOOOOOORRRRDDDDIIIINNNNAAAATTTTEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS RRRRaaaaddddiiiiaaaallll ccccoooooooorrrrddddiiiinnnnaaaatttteeee ssssyyyysssstttteeeemmmmssss are the sssspppphhhheeeerrrriiiiccccaaaallll and the ccccyyyylllliiiinnnnddddrrrriiiiccccaaaallll systems, explained shortly in more detail. You can import radial coordinate conversion functions by using the :radial tag: PPPPaaaaggggeeee 3333 MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) use Math::Trig ':radial'; ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); AAAAllllllll aaaannnngggglllleeeessss aaaarrrreeee iiiinnnn rrrraaaaddddiiiiaaaannnnssss. CCCCOOOOOOOORRRRDDDDIIIINNNNAAAATTTTEEEE SSSSYYYYSSSSTTTTEEEEMMMMSSSS CCCCaaaarrrrtttteeeessssiiiiaaaannnn coordinates are the usual rectangular (_x, _y, _z)-coordinates. Spherical coordinates, (_r_h_o, _t_h_e_t_a, _p_i), are three-dimensional coordinates which define a point in three-dimensional space. They are based on a sphere surface. The radius of the sphere is rrrrhhhhoooo, also known as the _r_a_d_i_a_l coordinate. The angle in the _x_y-plane (around the _z-axis) is tttthhhheeeettttaaaa, also known as the _a_z_i_m_u_t_h_a_l coordinate. The angle from the _z-axis is pppphhhhiiii, also known as the _p_o_l_a_r coordinate. The `North Pole' is therefore _0, _0, _r_h_o, and the `Bay of Guinea' (think of the missing big chunk of Africa) _0, _p_i/_2, _r_h_o. BBBBeeeewwwwaaaarrrreeee: some texts define _t_h_e_t_a and _p_h_i the other way round, some texts define the _p_h_i to start from the horizontal plane, some texts use _r in place of _r_h_o. Cylindrical coordinates, (_r_h_o, _t_h_e_t_a, _z), are three-dimensional coordinates which define a point in three-dimensional space. They are based on a cylinder surface. The radius of the cylinder is rrrrhhhhoooo, also known as the _r_a_d_i_a_l coordinate. The angle in the _x_y-plane (around the _z-axis) is tttthhhheeeettttaaaa, also known as the _a_z_i_m_u_t_h_a_l coordinate. The third coordinate is the _z, pointing up from the tttthhhheeeettttaaaa-plane. 3333----DDDD AAAANNNNGGGGLLLLEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS Conversions to and from spherical and cylindrical coordinates are available. Please notice that the conversions are not necessarily reversible because of the equalities like _p_i angles being equal to -_p_i angles. cartesian_to_cylindrical ($rho, $theta, $z) = cartesian_to_cylindrical($x, $y, $z); cartesian_to_spherical ($rho, $theta, $phi) = cartesian_to_spherical($x, $y, $z); PPPPaaaaggggeeee 4444 MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) cylindrical_to_cartesian ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z); cylindrical_to_spherical ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c, $theta, $z); Notice that when $z is not 0 $rho_s is not equal to $rho_c. spherical_to_cartesian ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi); spherical_to_cylindrical ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi); Notice that when $z is not 0 $rho_c is not equal to $rho_s. GGGGRRRREEEEAAAATTTT CCCCIIIIRRRRCCCCLLLLEEEE DDDDIIIISSSSTTTTAAAANNNNCCCCEEEESSSS You can compute spherical distances, called ggggrrrreeeeaaaatttt cccciiiirrrrcccclllleeee ddddiiiissssttttaaaannnncccceeeessss, by importing the great_circle_distance function: use Math::Trig 'great_circle_distance' $distance = great_circle_distance($theta0, $phi0, $theta1, $phi, [, $rho]); The _g_r_e_a_t _c_i_r_c_l_e _d_i_s_t_a_n_c_e is the shortest distance between two points on a sphere. The distance is in $rho units. The $rho is optional, it defaults to 1 (the unit sphere), therefore the distance defaults to radians. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS To calculate the distance between London (51.3N 0.5W) and Tokyo (35.7N 139.8E) in kilometers: use Math::Trig qw(great_circle_distance deg2rad); # Notice the 90 - latitude: phi zero is at the North Pole. @L = (deg2rad(-0.5), deg2rad(90 - 51.3)); @T = (deg2rad(139.8),deg2rad(90 - 35.7)); $km = great_circle_distance(@L, @T, 6378); The answer may be off by up to 0.3% because of the irregular (slightly aspherical) form of the Earth. PPPPaaaaggggeeee 5555 MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333)))) BBBBUUUUGGGGSSSS Saying use Math::Trig; exports many mathematical routines in the caller environment and even overrides some (sin, cos). This is construed as a feature by the Authors, actually... ;-) The code is not optimized for speed, especially because we use Math::Complex and thus go quite near complex numbers while doing the computations even when the arguments are not. This, however, cannot be completely avoided if we want things like asin(2) to give an answer instead of giving a fatal runtime error. AAAAUUUUTTTTHHHHOOOORRRRSSSS Jarkko Hietaniemi <_j_h_i@_i_k_i._f_i> and Raphael Manfredi <_R_a_p_h_a_e_l__M_a_n_f_r_e_d_i@_g_r_e_n_o_b_l_e._h_p._c_o_m>. PPPPaaaaggggeeee 6666